package com.atguigu.mybatis.test;

import com.atguigu.mybatis.entity.Employee;
import com.atguigu.mybatis.mapper.EmployeeMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class DemoTest02 {

    private SqlSession session;
    private Logger logger = LoggerFactory.getLogger(DemoTest02.class);

    @BeforeEach
    public void init() throws IOException {
        session = new SqlSessionFactoryBuilder()
                .build(Resources.getResourceAsStream("mybatis-config.xml"))
                .openSession();
    }

    @Test
    public void test01HelloWorld() {
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
        Employee employee = employeeMapper.selectEmployee(2);
        logger.debug("employee = " + employee);
    }

    @Test
    public void test02Logger() {
        logger.trace("hello trace");
        logger.debug("hello debug");
        logger.info("hello info");
        logger.warn("hello warn");
        logger.error("hello error");
    }

    @AfterEach
    public void clear() {
        session.commit();
        session.close();
    }
}
